<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="userName" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
                <tip>需要保证用户名的唯一性哦!</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="123456" class="layui-input">
                <tip>默认密码：123456</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">联系方式</label>
            <div class="layui-input-block">
                <input type="phone" name="phone" lay-verify="required" lay-reqtext="联系方式不能为空" placeholder="请输入联系方式" value="10086" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="required" lay-reqtext="用户类型不能为空">
                    <option value="">请选择</option>
                    <option value="0" >管理员</option>
                    <option value="1" >宿管员</option>
                    <!-- <option value="2">学生</option> 添加学生放在学生管理中 -->
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认新增</button>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','axios','layer','jquery'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            axios = layui.axios;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // 选择 type="button" 的 <button> 元素和 <input> 元素
            var DISABLED = 'layui-btn-disabled';

            $(':button').addClass(DISABLED); // 添加样式

            $(':button').attr('disabled', 'disabled');  // 添加属性
            axios({
                method:"post"
                ,url:'/user/add'
                ,data:data.field
            }).then(res=>{
              if(res.code==500){
                layer.msg(res.message,{
                    icon: 5,
                    time: 2000, //2秒关闭（如果不配置，默认是3秒）
                    anim:6
                });
                $(':button').removeClass(DISABLED);
                $(':button').removeAttr('disabled');
              }else{
                layer.msg(res.message, {
                    icon: 6,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                  layer.close(parentIndex);
                  layui.table.reload('user-table-id');
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');
                });
              }
            }).catch(res=>{
              layer.msg("添加用户失败",{
                  icon: 5,
                  time: 2000, //2秒关闭（如果不配置，默认是3秒）
                  anim:6
              });
              $(':button').removeClass(DISABLED);
              $(':button').removeAttr('disabled');
            });
            return false;
        });
    });
</script>